//
//  DMSettingVC.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/17.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMSettingVC.h"
#import "DMSettingCell.h"
#import "DMForgetVC.h"
#import "DMFeedbackVC.h"
#import "DMAboutUsVC.h"

@interface DMSettingVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation DMSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
}

-(void)startInit {
    
    self.navigationItem.title = @"设置";
    self.view.backgroundColor = DMDefaultColor;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [self footerView];
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"DMSettingCell" bundle:nil] forCellReuseIdentifier:@"DMSettingCell"];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 2) {
        return 3;
    }
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DMSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMSettingCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"DMSettingCell" owner:self options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *arr = @[@[@"修改密码"],
                     @[@"新消息提醒"],
                     @[@"意见反馈",@"清除缓存",@"版本升级"],
                     @[@"关于我们"]];
    cell.titleLb.text = arr[indexPath.section][indexPath.row];
    if (indexPath.section == 2 && indexPath.row == 1) {
        cell.secondLb.text = @"30K";
    }else if (indexPath.section == 2 && indexPath.row == 2) {
        cell.secondLb.text = @"V1.0.0";
    }else {
        cell.secondLb.text = @"";
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        //修改密码
        DMForgetVC *vc = [DMForgetVC new];
        vc.flag = 1;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.section == 1) {
        //新消息提醒
    }else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            //意见反馈
            DMFeedbackVC *vc = [DMFeedbackVC new];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 1) {
            //清除缓存
        }else if (indexPath.row == 2) {
            //版本升级
        }
    }else if (indexPath.section == 3) {
        //关于我们
        DMAboutUsVC *vc = [DMAboutUsVC new];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(UIView *)footerView {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 80)];
    view.backgroundColor = DMDefaultColor;
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(18, 20, ScreenWidth-36, 40)];
    button.backgroundColor = RGBCOLOR(242, 97, 97);
    [button setTitle:@"退出登录" forState:UIControlStateNormal];
    [button setTitleColor:DMWhiteColor forState:UIControlStateNormal];
    button.titleLabel.font = DMFontSize(14);
    button.layer.cornerRadius = 20;
    [button addTarget:self action:@selector(logoutClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    return view;
}

//退出登录
-(void)logoutClick {
    [[KuserInfo shareInstance]logout];

}

@end
